package com.yhp.service.impl.userserviceimpl;

import com.github.pagehelper.PageHelper;
import com.yhp.bo.CenterUserBO;
import com.yhp.bo.OrderItemsCommentBO;
import com.yhp.enums.YesOrNo;
import com.yhp.mapper.ItemsCommentsMapperCustom;
import com.yhp.mapper.OrderItemsMapper;
import com.yhp.mapper.OrderStatusMapper;
import com.yhp.mapper.OrdersMapper;
import com.yhp.pojo.OrderItems;
import com.yhp.pojo.OrderStatus;
import com.yhp.pojo.Orders;
import com.yhp.pojo.Users;
import com.yhp.service.BaseService;
import com.yhp.service.userservice.CenterCommentService;
import com.yhp.utils.PagedGridResult;
import com.yhp.vo.MyCommentVO;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CenterCommentServiceImpl extends BaseService implements CenterCommentService {
    @Autowired
    private OrderItemsMapper orderItemsMapper;
    @Autowired
    private ItemsCommentsMapperCustom itemsCommentsMapperCustom;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;


    @Autowired
    private Sid sid;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<OrderItems> queryPendingComment(String orderId) {
        OrderItems orderItems = new OrderItems();
        orderItems.setOrderId(orderId);
        return orderItemsMapper.select(orderItems);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveComment(String userId, String orderId, List<OrderItemsCommentBO> commentList) {
        //1.保存评价信息表 items_comments
        for (OrderItemsCommentBO bo : commentList) {
            bo.setCommentId(sid.nextShort());
        }
        //批量新增
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("commentList",commentList);
        itemsCommentsMapperCustom.saveComment(map);
        //2. 修改订单表，改为已评价 orders
        Orders orders = new Orders();
        orders.setId(orderId);
        orders.setIsComment(YesOrNo.YES.type);
        ordersMapper.updateByPrimaryKeySelective(orders);
        //3.修改订单状态表的评价时间  order_Status
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(orderId);
        orderStatus.setCommentTime(new Date());
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);

    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult queryMyComment(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        List<MyCommentVO> commentVOList = itemsCommentsMapperCustom.queryMyComments(map);

        return setPagedGrid(commentVOList,page);
    }
}